<?php
class ModelCatalogDownloadExtra extends Model {
	public function addDownloadExtra($data){
		$this->db->query("INSERT INTO ".DB_PREFIX."download_extra SET category_download_extra_id='".(int)$data['category_download_extra_id']."', status='".(int)$data['status']."', sort_order='".(int)$data['sort_order']."', file_name='".$this->db->escape($data['file_name'])."', date_add=NOW(), date_modify=NOW()");
		$download_extra_id = $this->db->getLastId();
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "download_extra SET image = '" . $this->db->escape($data['image']) . "' WHERE download_extra_id = '" . (int)$download_extra_id . "'");
		}
		foreach ($data['download_extra_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "download_extra_description SET download_extra_id = '" . (int)$download_extra_id . "', language_id = '" . (int)$language_id . "', short_description = '" . $this->db->escape($value['short_description']) . "', name = '" . $this->db->escape($value['name']) ."',description = '" . $this->db->escape($value['description']) . "'");
		}
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'download_extra_id=" . (int)$download_extra_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
	}
	public function editDownloadExtra($download_extra_id, $data){
		$this->db->query("UPDATE ".DB_PREFIX."download_extra SET category_download_extra_id='".(int)$data['category_download_extra_id']."', status='".(int)$data['status']."', sort_order='".(int)$data['sort_order']."', file_name='".$this->db->escape($data['file_name'])."', date_modify=NOW() WHERE download_extra_id=".(int)$download_extra_id);
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "download_extra SET image = '" . $this->db->escape($data['image']) . "' WHERE download_extra_id = '" . (int)$download_extra_id . "'");
		}
		$this->db->query("DELETE FROM ".DB_PREFIX."download_extra_description WHERE download_extra_id=".(int)$download_extra_id);
		foreach ($data['download_extra_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "download_extra_description SET download_extra_id = '" . (int)$download_extra_id . "', language_id = '" . (int)$language_id . "', short_description = '" . $this->db->escape($value['short_description']) . "', name = '" . $this->db->escape($value['name']) ."', description = '" . $this->db->escape($value['description']) . "'");
		}
		$this->db->query("DELETE FROM ".DB_PREFIX."url_alias WHERE query = 'download_extra_id=".(int)$download_extra_id . "'");
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'download_extra_id=" . (int)$download_extra_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
	}
	public function getDownloadExtra($download_extra_id){
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'download_extra_id=" . (int)$download_extra_id . "') AS keyword FROM " . DB_PREFIX . "download_extra v LEFT JOIN " . DB_PREFIX . "download_extra_description vd ON (v.download_extra_id =vd.download_extra_id) WHERE v.download_extra_id = '" . (int)$download_extra_id . "' AND vd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		return $query->row;
	}
	public function getDownloadExtraDescriptions($download_extra_id) {
		$download_extra_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "download_extra_description WHERE download_extra_id = '" . (int)$download_extra_id . "'");
		
		foreach ($query->rows as $result) {
			$download_extra_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'short_description'=> $result['short_description'],
				'description'      => $result['description']
			);
		}
		
		return $download_extra_description_data;
	}
	public function deleteDownloadExtra($download_extra_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "download_extra WHERE download_extra_id = '" . (int)$download_extra_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "download_extra_description WHERE download_extra_id = '" . (int)$download_extra_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'download_extra_id=" . (int)$download_extra_id . "'");
	} 
	
	public function getDownloadsExtra($data=array()){
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "download_extra v LEFT JOIN " . DB_PREFIX . "download_extra_description vd ON (v.download_extra_id = vd.download_extra_id) WHERE vd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		return $query->rows;
	}
	public function DeleteFileDownload($download_extra_id){
		$query = $this->db->query("UPDATE " . DB_PREFIX . "download_extra SET file_name ='' WHERE download_extra_id='".$download_extra_id."'" );
	}
}
?>